<template>
  <div class="shijuan-detail">
    <div class="layout-container">
      <!-- 左侧试卷部分 -->
      <div class="paper-container" ref="paperContainerRef">
        <!-- 试卷标题 -->
        <div class="paper-header">
          <h6 class="">{{ source || '试卷详情' }}</h6>
          <div class="paper-info">
            <span>共 {{ total || 0 }} 题</span>
          </div>
        </div>
        <!-- 题目列表 -->
        <div class="question-list">
          <div v-for="(item, index) in detail" :key="item.id" class="question-item"
            :ref="el => (questionRefs[item.orders] = el)" :class="{ 'material-question': item.typeCode === 'data' }">
            <!-- 题目部分 -->
            <div class="question-header">
              <div class="question-row" :class="{ 'material-layout': item.typeCode === 'data' }">
                <div class="left-section">
                  <span v-if="item.typeCode !== 'data'" class="question-index">{{
                    item.orders
                  }}</span>
                  <span class="question-type">
                    <!-- {{
											item.typeCode === 'data' ? '材料分析' : '单选题'
										}} -->
                    <!-- {{ item.questionType }} -->
                    <span v-if="item.typeCode === 'data'">材料分析</span>
                    <span v-else>
                      <span v-if="item.questionType == 1">单选题</span>
                      <span v-else-if="item.questionType == 2">多选题</span>
                      <span v-else-if="item.questionType == 3">不定向</span>
                      <span v-else-if="item.questionType == 99">判断题</span>
                    </span>
                  </span>
                </div>
                <div class="right-section">
                  <span class="question-text" v-html="item.questionText"></span>
                </div>
              </div>
            </div>

            <!-- 选项列表 -->
            <div v-if="item.typeCode !== 'data'" class="answer-section">
              <div class="options-list">
                <div v-for="option in item.options" :key="option.id" :class="{ isCorrect: option.isCorrect }"
                  class="option-item">
                  <span class="option-label">
                    <!-- {{ option.isCorrect }} -->

                    <span v-if="item.questionType == 1 || item.questionType == 2|| item.questionType == 3"> {{
                      option.optionLabel }}.</span>
                    <span v-else-if="item.questionType == 99">
                      <span v-if="option.optionLabel == 'A'">
                        <el-icon :size="20">
                          <circle-check />
                        </el-icon>
                      </span>
                      <span v-else-if="option.optionLabel == 'B'">
                        <el-icon :size="20">
                          <circle-close />
                        </el-icon>
                      </span>
                    </span>
                    <span v-else> {{
                      option.optionLabel }}.</span>

                  </span>
                  <div class="option-content">
                    <span class="option-text" v-html="option.optionText"></span>
                  </div>
                </div>
              </div>
            </div>
            <div class="detail-item">
              <label>解析：</label>
              <div v-html="item.analysis || '暂无解析'"></div>
            </div>
          </div>
        </div>
      </div>

      <!-- 右侧 Papertimu 组件 -->
      <div class="paper-container">
        <el-tabs v-model="activeName" @tab-click="handleClick">
          <el-tab-pane label="题目列表" name="first">
            <Papertimu @refresh="refresh" @scrollToQuestion="scrollToQuestion" style="width: 100%"
              :shijuanId="shijuanId" ref="papertimuRef" />
          </el-tab-pane>
          <el-tab-pane label="材料列表" name="second">
            <Analysis @crefresh="crefresh" style="width: 100%" :paperId="shijuanId" />
          </el-tab-pane>
        </el-tabs>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { findAll } from '../api/timu';
import { ref, onMounted } from 'vue';
import Papertimu from './Papertimu.vue';
import Analysis from './Analysis.vue';
const activeName = ref('first');

const props = defineProps({
  shijuanId: {
    type: String
  },
  source: {
    type: String
  }
});

const detail = ref([]);
const total = ref(0);
const paperContainerRef = ref<HTMLElement | null>(null);
const questionRefs = ref<{ [key: string]: HTMLElement }>({});

const init = () => {
  findAll({
    paperId: props.shijuanId,
    pageNum: 1,
    pageSize: -1,
    questionText: '',
    typeCode: ''
  }).then(res => {
    detail.value = res.data.records;
    total.value = res.data.total;
    // console.log(res.data);
  });
};
const papertimuRef = ref<any>(null);
const crefresh = () => {
  refresh();
  papertimuRef.value.loadQuestions();
};

const refresh = () => {
  init();
  // paper-container滚动条滚动到最后
  const paperContainer = document.querySelector('.paper-container');
  setTimeout(() => {
    paperContainer?.scrollTo({
      top: paperContainer.scrollHeight - 10,
      behavior: 'smooth'
    });
  }, 500);
};

const scrollToQuestion = (orders: string) => {
  const targetElement = questionRefs.value[orders];
  if (targetElement && paperContainerRef.value) {
    paperContainerRef.value.scrollTo({
      top: targetElement.offsetTop - 20, // 20px 的上边距
      behavior: 'smooth'
    });

    // 添加高亮效果
    targetElement.classList.add('highlight-question');
    setTimeout(() => {
      targetElement.classList.remove('highlight-question');
    }, 2000);
  }
};

onMounted(() => {
  init();
});
</script>

<style lang="scss" scoped>
.question-text :deep(p img) {
  vertical-align: middle !important;
}

.detail-item :deep(p img) {
  vertical-align: middle !important;

}

.shijuan-detail {
  min-height: 95vh;
  background-color: #f6f8fa;
  padding: 16px;

  .layout-container {
    display: flex;
    gap: 16px;
    margin: 0 auto;
    height: calc(92vh);

    .paper-container {
      background: #fff;
      padding: 32px 40px;
      border-radius: 16px;
      box-shadow: 0 4px 20px rgba(0, 0, 0, 0.06);
      overflow-y: auto;

      &:first-child {
        flex: 0 0 40%;
      }

      &:last-child {
        flex: 1;
        min-width: 0;
      }
    }
  }

  .paper-header {
    text-align: center;
    padding-bottom: 24px;
    margin-bottom: 32px;
    border-bottom: 1px solid #edf2f7;
    position: relative;

    h6 {
      font-size: 24px;
      color: #1a202c;
      font-weight: 600;
      margin-bottom: 12px;
      line-height: 1.4;
    }

    .paper-info {
      display: inline-flex;
      align-items: center;
      padding: 6px 16px;
      background: #edf2f7;
      border-radius: 20px;
      font-size: 14px;
      color: #4a5568;
      font-weight: 500;

      &::before {
        content: '';
        display: inline-block;
        width: 6px;
        height: 6px;
        background: #4299e1;
        border-radius: 50%;
        margin-right: 8px;
      }
    }
  }

  .question-list {
    .question-item {
      margin-bottom: 24px;
      padding: 24px;
      border-radius: 12px;
      background: #fff;
      border: 1px solid #e2e8f0;
      transition: all 0.3s ease;

      &:hover {
        box-shadow: 0 8px 30px rgba(0, 0, 0, 0.08);
        border-color: #cbd5e0;
        transform: translateY(-1px);
      }

      &.material-question {
        background: #f8fafc;
        border: 1px solid #e2e8f0;

        .question-row {
          display: flex;
          flex-direction: column;
          gap: 16px;

          .left-section {
            width: 100%;
            border-bottom: 1px solid #e2e8f0;
            padding-bottom: 12px;
            margin-bottom: 8px;

            .question-type {
              background: #ebf8ff;
              color: #3182ce;
              font-size: 14px;
              padding: 6px 12px;
              border-radius: 6px;
            }
          }

          .right-section {
            padding: 0;
            margin: 0;

            .question-text {
              font-size: 15px;
              line-height: 1.8;
              color: #2d3748;
            }
          }
        }
      }

      .question-row {
        display: flex;
        align-items: flex-start;
        gap: 12px;

        .left-section {
          width: 100px;
          flex-shrink: 0;
          display: flex;
          align-items: center;
          gap: 10px;

          .question-index {
            min-width: 28px;
            height: 28px;
            line-height: 28px;
            text-align: center;
            background: #1890ff;
            color: #fff;
            border-radius: 6px;
            font-size: 14px;
            font-weight: 500;
            box-shadow: 0 2px 4px rgba(24, 144, 255, 0.15);
          }

          .question-type {
            padding: 4px 10px;
            background: #f0f7ff;
            color: #1890ff;
            border-radius: 6px;
            font-size: 13px;
            font-weight: 500;
            white-space: nowrap;
          }
        }

        .right-section {
          flex: 1;
          min-width: 0;
          margin-top: 4px;
        }
      }

      .answer-section {
        margin-top: 20px;
        background: #f8fafc;
        padding: 20px;
        border-radius: 10px;
        border: 1px solid #e2e8f0;

        .options-list {
          display: flex;
          flex-direction: column;
          gap: 12px;

          .option-item {
            display: flex;
            align-items: flex-start;
            padding: 12px 16px;
            border-radius: 8px;
            background: #fff;
            border: 1px solid #e2e8f0;
            transition: all 0.2s ease;

            :deep(img) {
              vertical-align: middle !important;
            }

            &:hover {
              border-color: #4299e1;
              background: #f7fafc;
            }

            .option-label {
              width: 26px;
              height: 26px;
              line-height: 26px;
              text-align: center;
              background: #edf2f7;
              color: #4a5568;
              border-radius: 6px;
              margin-right: 12px;
              font-weight: 500;
              font-size: 13px;
              flex-shrink: 0;
            }

            .option-content {
              flex: 1;
              padding: 3px 0;

              .option-text {
                // color: #4a5568;
                line-height: 1.6;
                font-size: 14px;
              }
            }
          }
        }
      }

      // 单选题样式
      &:not(.material-question) {
        .question-type {
          background: #f0f7ff;
          color: #1890ff;
        }

        .question-index {
          background: linear-gradient(135deg, #1890ff, #40a9ff);
          box-shadow: 0 2px 6px rgba(24, 144, 255, 0.2);
        }
      }

      // 材料题样式
      &.material-question {
        background: #f8fafc;
        border: 1px solid #e2e8f0;

        .question-type {
          background: #ebf8ff;
          color: #3182ce;
        }
      }

      // 多选题样式
      &[data-type='b'] {
        .question-type {
          background: #f6f0ff;
          color: #722ed1;
        }

        .question-index {
          background: linear-gradient(135deg, #722ed1, #b37feb);
          box-shadow: 0 2px 6px rgba(114, 46, 209, 0.2);
        }
      }

      // 判断题样式
      &[data-type='c'] {
        .question-type {
          background: #f0fff6;
          color: #13c2c2;
        }

        .question-index {
          background: linear-gradient(135deg, #13c2c2, #36cfc9);
          box-shadow: 0 2px 6px rgba(19, 194, 194, 0.2);
        }
      }

      // 填空题样式
      &[data-type='d'] {
        .question-type {
          background: #fff7e6;
          color: #fa8c16;
        }

        .question-index {
          background: linear-gradient(135deg, #fa8c16, #ffc069);
          box-shadow: 0 2px 6px rgba(250, 140, 22, 0.2);
        }
      }

      // 简答题样式
      &[data-type='e'] {
        .question-type {
          background: #fff1f0;
          color: #f5222d;
        }

        .question-index {
          background: linear-gradient(135deg, #f5222d, #ff7875);
          box-shadow: 0 2px 6px rgba(245, 34, 45, 0.2);
        }
      }

      // 添加高亮动画样式
      &.highlight-question {
        animation: highlight 2s ease-out;
      }
    }
  }

  :deep(img) {
    max-width: 100%;
    height: auto;
    border-radius: 10px;
    margin: 16px 0;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
  }

  :deep(table) {
    width: 100%;
    margin: 16px 0;
    border-radius: 10px;
    overflow: hidden;
    border: 1px solid #e2e8f0;
    background: #fff;

    th {
      background: #f8fafc;
      color: #2d3748;
      font-weight: 600;
      padding: 12px 16px;
      font-size: 14px;
    }

    td {
      padding: 12px 16px;
      border-top: 1px solid #e2e8f0;
      color: #4a5568;
      font-size: 14px;
    }
  }

  :deep(pre) {
    background: #f8fafc;
    padding: 16px;
    border-radius: 10px;
    margin: 16px 0;
    font-family: 'Fira Code', Consolas, Monaco, monospace;
    font-size: 14px;
    line-height: 1.6;
    overflow-x: auto;
    border: 1px solid #e2e8f0;
  }

  :deep(.el-tabs__content) {
    overflow: hidden;
    width: 100%;
  }

  :deep(.el-tab-pane) {
    width: 100%;
    min-width: 0;
  }
}

@keyframes highlight {
  0% {
    background-color: rgba(24, 144, 255, 0.1);
    border-color: #1890ff;
  }

  100% {
    background-color: #fff;
    border-color: #e2e8f0;
  }
}

.isCorrect {
  background-color: #6fc643 !important;
  color: #fff !important;
}
</style>
